package com.ruoyi.web.controller.flowable;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.dto.FlowEngineDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProcessTemplates;
import com.ruoyi.system.service.IProcessTemplatesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审批模板Controller
 *
 * @author ruoyi
 * @date 2024-03-27
 */
@RestController
@RequestMapping("/flowable/form")
public class ProcessTemplatesController extends BaseController
{
    @Autowired
    private IProcessTemplatesService processTemplatesService;

    /**
     * 查询审批模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:templates:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProcessTemplates processTemplates)
    {
//        startPage();
        List<ProcessTemplates> list = processTemplatesService.selectProcessTemplatesList(processTemplates);
        return getDataTable(list);
    }


    /**
     * 获取审批模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:templates:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") String templateId)
    {
        return success(processTemplatesService.selectProcessTemplatesByTemplateId(templateId));
    }

    /**
     * 发布审批模板
     */
    @PreAuthorize("@ss.hasPermi('system:templates:add')")
    @Log(title = "审批模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FlowEngineDTO flowEngineDTO)
    {
        return processTemplatesService.insertProcessTemplates(flowEngineDTO);
    }

    /**
     * 修改审批模板状态（0：停用 1：启用）
     */
    @PreAuthorize("@ss.hasPermi('system:templates:edit')")
    @Log(title = "审批模板", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody ProcessTemplates processTemplates)
    {
        return toAjax(processTemplatesService.updateProcessTemplatesStatus(processTemplates));
    }


    /**
     * 移动审批模板
     */
    @PreAuthorize("@ss.hasPermi('system:templates:edit')")
    @Log(title = "审批模板", businessType = BusinessType.UPDATE)
    @PutMapping("/updateTemplatesGroup")
    public AjaxResult updateProcessTemplatesGroup(@RequestBody ProcessTemplates processTemplates)
    {
        return toAjax(processTemplatesService.updateProcessTemplatesGroup(processTemplates));
    }

    /**
     * 修改审批模板
     */
    @PreAuthorize("@ss.hasPermi('system:templates:edit')")
    @Log(title = "审批模板", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult update(@RequestBody ProcessTemplates processTemplates)
    {
        return processTemplatesService.updateProcessTemplates(processTemplates);
    }

    /**
     * 删除审批模板
     */
    @PreAuthorize("@ss.hasPermi('system:templates:remove')")
    @Log(title = "审批模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{templateId}")
    public AjaxResult remove(@PathVariable String templateId)
    {
        return toAjax(processTemplatesService.deleteProcessTemplatesByTemplateId(templateId));
    }

    /**
     * 查询菜单是否已经有关联的模板
     */
    @PreAuthorize("@ss.hasPermi('system:templates:query')")
    @GetMapping(value = "/selectMenuTemplate/{menuId}")
    public AjaxResult selectMenuTemplate(@PathVariable("menuId") Long menuId)
    {
        return processTemplatesService.selectProcessTemplatesByMenuId(menuId);
    }
}
